package spade;

public class Behaviour extends MessageReceiver
{
	int	      exitcode	 = 0;
	Behaviour	myParent	= null;
	Agent	  myAgent;
	boolean	  forceKill	 = false;

	public Behaviour()
	{

	}
	public Behaviour root()
	{
		if (myParent != null)
		{
			return myParent.root();
		}
		else
		{
			return this;
		}
	}
	public boolean done()
	{
		return false;
	}

	public int process()
	{
		throw new RuntimeException("Not Implemented");
	}

    @SuppressWarnings("deprecation")
	public void kill()
	{
		// si ya se ha pedido un kill antes, usamos stop. Si no, intentamos
		// salir limpiamente
		onEnd();
		if (this.forceKill == false)
		{
			this.forceKill = true;
		}
		else
		{
			this.stop();
		}
	}
	public void start()
	{
		onStart();
		super.start();
	}
	public void onStart()
	{
	}
	public void onEnd()
	{
	}
	public int exitCode()
	{
		return exitcode;
	}
	public void run()
	{
		// if not self.myAgent._running:
		// # Get condition and wait for the other behaviours
		// self.myAgent.behavioursGo.acquire()
		// self.myAgent.behavioursGo.wait()
		// self.myAgent.behavioursGo.release()
		while (done() == false && this.forceKill == false)
		{
			this.exitcode = this.process();
		}
		// self.myAgent.removeBehaviour(self)
	}
	public Agent getMyAgent()
	{
		return myAgent;
	}
	public void setMyAgent(Agent myAgent)
	{
		this.myAgent = myAgent;
		// self.DEBUG = self.myAgent.DEBUG
		// try:
		// self.setName(str(self.myAgent.getName()) + " Behaviour")
		// except:
		// pass
	}
	public Behaviour getMyParent()
	{
		return myParent;
	}
	public void setMyParent(Behaviour myParent)
	{
		this.myParent = myParent;
	}

	public void setTemplate(MessageTemplate template)
	{
		if (this.myAgent != null)
		{
			// self.myAgent._behaviourList[self] = template
		}
	}

}
